package handler

import (
	"net/http"

	"gitee.com/thewalkers/refactoring-code/model"
	"gitee.com/thewalkers/refactoring-code/model/apperrors"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type addSalariedEmployeeParams struct {
	Name    string  `json:"employee_name" binding:"required"`
	Address string  `json:"employee_address" binding:"required"`
	Salary  float64 `json:"salary" binding:"required"`
}

func (h *Handler) AddSalariedEmployee(ctx *gin.Context) {
	var req addSalariedEmployeeParams

	if ok := bindData(ctx, &req); !ok {
		return
	}

	e := &model.Employee{
		Name:    req.Name,
		Address: req.Address,
	}

	s := &model.SalariedClassification{
		Salary: req.Salary,
	}

	err := h.AddSalaryEmployeeTransactionService.Create(ctx, e, s)
	if err != nil {
		zap.L().Error("service failed to add salary employee transaction", zap.Error(err))
		ctx.JSON(apperrors.Status(err), gin.H{
			"error": err,
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"msg": createEmployeeSuccess,
	})
}
